<?php
// +----------------------------------------------------------------------
// | 零云 [ 简单 高效 卓越 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.lingyun.net All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com>
// +----------------------------------------------------------------------

require_once APP_PATH . 'Common/Common/developer.php'; //加载开发者二次开发公共函数库

/**
 * 处理插件钩子
 * @param string $hook   钩子名称
 * @param mixed $params 传入参数
 * @return void
 * @author jry <598821125@qq.com>
 */
function hook($hook, $params = array())
{
    $result = \Think\Hook::listen($hook, $params);
}

/**
 * 获取插件类的类名
 * @param strng $name 插件名
 * @author jry <598821125@qq.com>
 */
function get_addon_class($name)
{
    $class = "Addons\\{$name}\\{$name}Addon";
    return $class;
}

/**
 * 插件显示内容里生成访问插件的url
 * @param string $url url
 * @param array $param 参数
 * @author jry <598821125@qq.com>
 */
function addons_url($url, $param = array())
{
    return D('Admin/Addon')->getAddonUrl($url, $param);
}

/**
 * 兼容Nginx
 * @return array
 * @author jry <598821125@qq.com>
 */
if (!function_exists('getallheaders')) {
    function getallheaders()
    {
        foreach ($_SERVER as $name => $value) {
            if (substr($name, 0, 5) == 'HTTP_') {
                $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
            }
        }
        return $headers;
    }
}

/**
 * POST数据提前处理
 * @return array
 * @author jry <598821125@qq.com>
 */
function format_data($data = null)
{
    //解析数据类似复选框类型的数组型值
    if (!$data) {
        $data = $_POST;
    }
    $data_object = new \Util\Date;
    foreach ($data as $key => $val) {
        if (!is_array($val)) {
            $val = trim($val);
            if ($data_object->checkDatetime($val)) {
                $data[$key] = strtotime($val);
            } else if ($data_object->checkDatetime($val, 'Y-m-d H:i')) {
                $data[$key] = strtotime($val);
            } else if ($data_object->checkDatetime($val, 'Y-m-d')) {
                $data[$key] = strtotime($val);
            } else {
                $data[$key] = $val;
            }
        } else {
            $data[$key] = implode(',', $val);
        }
    }
    return $data;
}

/**
 * 获取所有数据并转换成一维数组
 * @author jry <598821125@qq.com>
 */
function select_list_as_tree($model, $map = null, $extra = null, $key = 'id',$title = 'title')
{
    //获取列表
    $con['status'] = array('eq', 1);
    if ($map) {
        $con = array_merge($con, $map);
    }
    $model_object = D($model);
    if (in_array('sort', $model_object->getDbFields())) {
        $list = $model_object->where($con)->order('sort asc, id asc')->select();
    } else {
        $list = $model_object->where($con)->order('id asc')->select();
    }

    //转换成树状列表(非严格模式)
    $tree = new \Util\Tree();
    $list = $tree->array2tree($list, $title, 'id', 'pid', 0, false);

    if ($extra) {
        $result[0] = $extra;
    }

    //转换成一维数组
    foreach ($list as $val) {
        $result[$val[$key]] = $val['title_show'];
    }
    return $result;
}

/**
 * 解析文档内容
 * @param string $str 待解析内容
 * @return string
 * @author jry <598821125@qq.com>
 */
function parse_content($str)
{
    // 将img标签的src改为lazy-src用户前台图片lazyload加载
    if (C('STATIC_DOMAIN')) {
        $tmp = preg_replace('/<img.*?src="(.*?Uploads.*?)"(.*?)>/i', "<img class='lazy lazy-fadein img-responsive' style='display:inline-block;' data-src='" . C('STATIC_DOMAIN') . "$1'>", $str);
        $tmp = preg_replace('/<img.*?src="(\/.*?)"(.*?)>/i', "<img class='img-responsive' style='display:inline-block;' src='" . C('STATIC_DOMAIN') . "$1'>", $tmp);
    } else {
        $domain = (is_ssl() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'];
        $tmp    = preg_replace('/<img.*?src="(.*?Uploads.*?)"(.*?)>/i', "<img class='lazy lazy-fadein img-responsive' style='display:inline-block;' data-src='" . $domain . "$1'>", $str);
        $tmp    = preg_replace('/<img.*?src="(\/.*?)"(.*?)>/i', "<img class='img-responsive' style='display:inline-block;' src='" . $domain . "$1'>", $tmp);
    }
    return $tmp;
}

/**
 * 字符串截取(中文按2个字符数计算)，支持中文和其他编码
 * @static
 * @access public
 * @param str $str 需要转换的字符串
 * @param str $start 开始位置
 * @param str $length 截取长度
 * @param str $charset 编码格式
 * @param str $suffix 截断显示字符
 * @return str
 */
function cut_str($str, $start, $length, $charset = 'utf-8', $suffix = true)
{
    return \Util\Str::cutStr(
        $str, $start, $length, $charset, $suffix
    );
}

/**
 * 过滤标签，输出纯文本
 * @param string $str 文本内容
 * @return string 处理后内容
 * @author jry <598821125@qq.com>
 */
function html2text($str)
{
    return \Util\Str::html2text($str);
}

/**
 * 友好的时间显示
 * @param int    $sTime 待显示的时间
 * @param string $type  类型. normal | mohu | full | ymd | other
 * @param string $alt   已失效
 * @return string
 * @author jry <598821125@qq.com>
 */
function friendly_date($sTime, $type = 'mohu', $alt = 'false')
{
    $date = new \Util\Date((int) $sTime);
    return $date->friendlyDate($type, $alt);
}

/**
 * 用于生成插入datetime类型字段用的字符串
 * @param string $str 支持偏移字符串
 */
function datetime($str = 'now')
{
    return @date("Y-m-d H:i:s", strtotime($str));
}

/**
 * 时间戳格式化
 * @param int $time
 * @return string 完整的时间显示
 * @author jry <598821125@qq.com>
 */
function time_format($time = null, $format = 'Y-m-d H:i')
{
    $time = $time === null ? time() : intval($time);
    return date($format, $time);
}

/**
 * 系统非常规MD5加密方法
 * @param  string $str 要加密的字符串
 * @return string
 * @author jry <598821125@qq.com>
 */
function user_md5($str='', $auth_key='')
{
    if (!$auth_key) {
        $auth_key = C('AUTH_KEY') ?: 'OpenCMF';
    }
    return '' === $str ? '' : md5(sha1($str) . $auth_key);
}

/**
 * 检测用户是否登录
 * @return integer 0-未登录，大于0-当前登录用户ID
 * @author jry <598821125@qq.com>
 */
function is_login()
{
    return D('Admin/User')->is_login();
}

/**
 * 检测用户是否VIP
 * @return integer VIP等级
 * @author jry <598821125@qq.com>
 */
function is_vip($uid)
{
    if (D('Admin/Module')->where('name="Vip" and status="1"')->count()) {
        $uid = $uid ? $uid : is_login();
        return D('Vip/Index')->isVip($uid);
    }
    return false;
}

/**
 * 获取上传文件路径
 * @param  int $id 文件ID
 * @return string
 * @author jry <598821125@qq.com>
 */
function get_cover($id = null, $type = null)
{
    return D('Admin/Upload')->getCover($id, $type);
}

/**
 * 自动生成URL，支持在后台生成前台链接
 * @param string $url URL表达式，格式：'[模块/控制器/操作#锚点@域名]?参数1=值1&参数2=值2...'
 * @param string|array $vars 传入的参数，支持数组和字符串
 * @param string|boolean $suffix 伪静态后缀，默认为true表示获取配置值
 * @param boolean $domain 是否显示域名
 * @return string
 * @author jry <598821125@qq.com>
 */
function oc_url($url = '', $vars = '', $suffix = true, $domain = true)
{
    $url = U($url, $vars, $suffix, $domain);
    if (MODULE_MARK === 'Admin') {
        $url_model = D('Admin/Config')->where(array('name' => 'URL_MODEL'))->getField('value');
        switch ($url_model) {
            case '1':
                $result = strtr($url, array('admin.php?s=' => 'index.php'));
                break;
            case '2':
                $result = strtr($url, array('admin.php?s=/' => ''));
                break;
            case '3':
                $result = strtr($url, array('admin.php' => 'index.php'));
                break;
            default:
                $result = strtr($url, array('admin.php' => 'index.php'));
                break;
        }
        return $result;
    } else {
        return $url;
    }
}

/**
 * 检测是否使用手机访问
 * @access public
 * @return bool
 */
function is_wap()
{
    if (isset($_SERVER['HTTP_VIA']) && stristr($_SERVER['HTTP_VIA'], "wap")) {
        return true;
    } elseif (isset($_SERVER['HTTP_ACCEPT']) && strpos(strtoupper($_SERVER['HTTP_ACCEPT']), "VND.WAP.WML")) {
        return true;
    } elseif (isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE'])) {
        return true;
    } elseif (isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/(blackberry|configuration\/cldc|hp |hp-|htc |htc_|htc-|iemobile|kindle|midp|mmp|motorola|mobile|nokia|opera mini|opera |Googlebot-Mobile|YahooSeeker\/M1A1-R2D2|android|iphone|ipod|mobi|palm|palmos|pocket|portalmmm|ppc;|smartphone|sonyericsson|sqh|spv|symbian|treo|up.browser|up.link|vodafone|windows ce|xda |xda_)/i', $_SERVER['HTTP_USER_AGENT'])) {
        return true;
    } else {
        return false;
    }
}

/**
 * 是否微信访问
 * @return bool
 * @author jry <598821125@qq.com>
 */
function is_weixin()
{
    if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
        return true;
    } else {
        return false;
    }
}

function wxpay($info,$data) {
    require_once APP_PATH."/wxlib/WxPay.Api.php";
    require_once APP_PATH."/wxlib/WxPay.JsApiPay.php";
    require_once APP_PATH.'/wxlib/log.php';
    $data['totalprice'] = $data['realprice']*100;
    $logHandler= new CLogFileHandler(APP_PATH."/wxlogs/".date('Y-m-d').'.log');
    $log = Log::Init($logHandler, 15);
    $tools = new JsApiPay();
    $openId = $tools->GetOpenid();

    if ($data['out_trade_no']) {
        $out_trade_no = $data['out_trade_no'];
    }else{
        $out_trade_no = WxPayConfig::MCHID.date("ynj").$data['id'];
    }

    
    $post_object = D('Admin/Inviteorder');
    $pay_info['openid'] = $openId;
    $pay_info['out_trade_no'] = $out_trade_no;
    $post_object->where('id='.$data['id'])->data($pay_info)->save();
    
    //②、统一下单
    $input = new WxPayUnifiedOrder();
    $input->SetBody($info['title']);
    $input->SetAttach($data['jointime']);
    $input->SetOut_trade_no($out_trade_no);
    $input->SetTotal_fee($data['totalprice']);
    $input->SetTime_start(date("YmdHis"));
    $input->SetTime_expire(date("YmdHis", time() + 600));
    $input->SetNotify_url("http://wx.intable.net/wxpay/notify");
    $input->SetTrade_type("JSAPI");
    $input->SetOpenid($openId);
    $order = WxPayApi::unifiedOrder($input);
    $jsApiParameters = $tools->GetJsApiParameters($order);
    return ($jsApiParameters);
}

function wxnofity() {
    require_once APP_PATH."/wxlib/mynotify.php";
    $notify = new PayNotifyCallBack();
    $notify->Handle(false);
}



function send_batch_sms($mobile,$class,$address) {
    $now = time();
    require_once APP_PATH."/alilib/SignatureHelper.php";
    $params = array ();
    // fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
    $accessKeyId = "LTAIR8UO0pxpfLRJ";
    $accessKeySecret = "Ma2TOnhMUkratcPjJ2uj4YLmalkJwW";
    $params["TemplateCode"] = "SMS_130840997";
    $params["PhoneNumberJson"] = array();
    $params["SignNameJson"] = array();
    $params["TemplateParamJson"] = array();
    $mobile = str_replace('，',',',$mobile);
    $mobile = str_replace("\n",',',$mobile);
    $mobile = str_replace("\r",',',$mobile);

    $mobile_array = explode(',',$mobile);
    $result['status'] = 'false';
    $result['msg'] = '';
    if (count($mobile_array)>100) {
        $result['msg'] = '单次发送号码不能超过100个';
        return $result;
    }



    foreach ($mobile_array as $val) {
        if (strlen($val)!=11) {
            continue;
        }
        array_push($params["PhoneNumberJson"],$val);
        array_push($params["SignNameJson"],'intable高端体验');
        array_push($params["TemplateParamJson"],array("class"=> $class,"address"=> $address));
    }

    // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
    $params["TemplateParamJson"]  = json_encode($params["TemplateParamJson"], JSON_UNESCAPED_UNICODE);
    $params["SignNameJson"] = json_encode($params["SignNameJson"], JSON_UNESCAPED_UNICODE);
    $params["PhoneNumberJson"] = json_encode($params["PhoneNumberJson"], JSON_UNESCAPED_UNICODE);

    if(!empty($params["SmsUpExtendCodeJson"] && is_array($params["SmsUpExtendCodeJson"]))) {
        $params["SmsUpExtendCodeJson"] = json_encode($params["SmsUpExtendCodeJson"], JSON_UNESCAPED_UNICODE);
    }
    // 初始化SignatureHelper实例用于设置参数，签名以及发送请求
    $helper = new SignatureHelper();

    // 此处可能会抛出异常，注意catch
    $content = $helper->request(
        $accessKeyId,
        $accessKeySecret,
        "dysmsapi.aliyuncs.com",
        array_merge($params, array(
            "RegionId" => "cn-hangzhou",
            "Action" => "SendBatchSms",
            "Version" => "2017-05-25",
        ))
        // fixme 选填: 启用https
        // ,true
    );

    if ($content['Message']=='OK') {
        $result['status'] = 'true';
        return $result;
    }else{
        $result['status'] = 'false';
        $result['msg'] = '发送失败';
        return $result;
    }


}

function send_sms($mobile,$type,$id='',$is_ajax=1) {
    $now = time();
    require_once APP_PATH."/alilib/SignatureHelper.php";
    $params = array ();
    // fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
    $accessKeyId = "LTAIR8UO0pxpfLRJ";
    $accessKeySecret = "Ma2TOnhMUkratcPjJ2uj4YLmalkJwW";
    // fixme 必填: 短信接收号码
    $params["PhoneNumbers"] = $mobile;
    // fixme 必填: 短信签名，应严格按"签名名称"填写，请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
    $params["SignName"] = "intable高端体验";
    // fixme 必填: 短信模板Code，应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
//SMS_130295026 您的验证码${code}，该验证码5分钟内有效，请勿泄漏于他人！

    //SMS_128651092 您好，欢迎使用平台预订课程，您已预订了${time}的 ${class}课程，地址是${address}，我们会在开始前一天以短信方式再次提醒，谢谢！
//SMS_129762979 您好，明天您预订的${class} 课程，即将开始，地址是${address}，我们期待您的大驾光临，不见不散哦，谢谢！
    

    // fixme 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
    
    switch ($type) {
        case "1"://验证码
            $authcode = mt_rand(1000,9999);
            $params["TemplateCode"] = "SMS_130295026";
            $params['TemplateParam'] = Array (
                "code" => $authcode
            );
            $data['mobile'] = $mobile;
            $data['status'] = 0;
            $auth_object   = D('Admin/Mobileauth');
            $get_data = $auth_object->where($data)->order('id desc')->find();
            if ($now-$get_data['sendtime']<=60) {
                echo '发送频率过高，请耐心等待';
                exit;
            }
            $data['sendtime'] = time();
            $data['authcode'] = $authcode;
            
            break;
        case "2"://报名成功
            $inviteorder_object = D('Admin/Inviteorder');
            $invite_object = D('Admin/Invitation');
            $con['id'] = $id;
            $info = $inviteorder_object->where($con)->find();
            $icon['id'] = $info['inviteid'];
            $data = $invite_object->where($icon)->find();
            $params["TemplateCode"] = "SMS_128651092";
            $params['TemplateParam'] = Array (
                "time" => date("Y-m-d H:i:s",$info['jointime']),
                "class"=> $data['title'],
                "address"=> $data['address']

            );
            break;
        case "3":
            $invite_object = D('Admin/Invitation');
            $icon['id'] = $id;
            $data = $invite_object->where($icon)->find();
            $params["TemplateCode"] = "SMS_130840997";
            $params['TemplateParam'] = Array (
                "class"=> $data['title'],
                "address"=> $data['address']
            );
            break;
        case "4":
            $params['TemplateCode'] = "SMS_130295026";
            $params['TemplateParam'] = Array(
                "code" => '9527'
            );
            break;
    }
    // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
    if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
        $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
    }

    // 初始化SignatureHelper实例用于设置参数，签名以及发送请求
    $helper = new SignatureHelper();

    // 此处可能会抛出异常，注意catch
    $content = $helper->request(
        $accessKeyId,
        $accessKeySecret,
        "dysmsapi.aliyuncs.com",
        array_merge($params, array(
            "RegionId" => "cn-hangzhou",
            "Action" => "SendSms",
            "Version" => "2017-05-25",
        ))
        // fixme 选填: 启用https
        // ,true
    );
    if ($content['Message']=='OK') {
        if ($type=='1') {
            $auth_object->add($data);
        }
        if ($is_ajax) {
            echo "1";
        }else{
            return true;
        }
    }else{
        //print_r($params);
        //print_r($content);
        //die();
        if ($is_ajax) {
            echo "0";
        }else{
            return true;
        }
    }
}
